package com.aaa.customer;

import com.aaa.entity.OOrder;
import com.aaa.feign.OrderFeign;
import com.aaa.vo.Result;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.List;

/**
 * @Description: 用户充值的消费者
 * @Author: Van.Planifolia
 * @CreateTime: 2023-03-08  20:04
 * @Version: 1.0
 */
@Component
public class OrderCustomer {
    // 引入salafoujie
    @Autowired
    private StringRedisTemplate redis;
    @Autowired
    private OrderFeign orderFeign;


    /**
     * 消费方法，消费充值队列中的一条记录
     *
     * @param message 消息
     * @param channel 批准消息
     */
    @RabbitListener(queues = {"orderSurviveQueue"})
    public void chargeOrderCustomer(Message message, Channel channel) throws IOException {
        String correlationId = message.getMessageProperties().getCorrelationId();
        try {
            //处理订单支付成功消息
            // 如果雷迪斯中有这个消息id就证明该消息确实付款了可用消费消息
            if (StringUtils.hasText(redis.opsForValue().get(correlationId))) {
                System.out.println("消息,id:" + correlationId + "内容:" + new String(message.getBody()) + "确实消费了");
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
                redis.delete(correlationId);
            } else {
                // 否则就抛出异常重新入队
                throw new RuntimeException();
            }
        } catch (Exception e) {
            //消息重新入队
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), true, true);
        }
    }


    /**
     * 充值死信队列的消费者也就是超时自动清除
     *
     * @param message 消息对象
     * @param channel 确认机制
     */
    @RabbitListener(queues = {"orderDeadQueue"})
    public void customerDeadChargeOrder(Message message, Channel channel) {
        // 取出消息的id
        String correlationId = message.getMessageProperties().getCorrelationId();
        // 打印日志
        System.out.println("消息,id:" + correlationId + "内容:" + new String(message.getBody()) + "被超时清除了");
        // 处理清除订单业务
//        UserCharge userCharge = userChargeFeign.selectChargeOrder(correlationId);
        List<OOrder> bycode = orderFeign.findBycode(correlationId);

        // 如果订单的状态还是为0.那么我们就要将他标记为已经超时
        // 否则就不做任何操作
        try {
            if (1 == bycode.get(0).getState()) {
                for (OOrder oOrder : bycode) {
                    int i = orderFeign.updateSumNum(String.valueOf(oOrder.getNum()), Integer.valueOf(oOrder.getGoodsid()));
                    Result updete = orderFeign.updete(String.valueOf(oOrder.getId()));
                }

                System.out.println("消息,id:" + correlationId + "内容:" + new String(message.getBody()) + "被超时清除了");
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
